package name.luoyong.base.ds;


import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.annotation.Transactional;

public abstract class  AbstractService<Entity extends AbstractEntity,Dao extends AbstractDao<Entity>> {

	@Autowired
	private Dao dao;
	
	@PersistenceContext
	protected EntityManager em;
	@Autowired
	protected JdbcTemplate jdbcTemplate;
	
	//@Transactional
	public void add(Entity entity) {
		System.out.println("begin AbstractService.add() : ");
		dao.persist(entity);
		System.out.println("end AbstractService.add() : "+entity);
	}
	
	public Entity get(Long id) {
		return dao.find(id);
	}
	
	@Transactional
	public void merge(Entity entity) {
		dao.merge(entity);
	}
	
	@Transactional
	public void remove(Entity entity) {
		dao.remove(entity);
	}
	
	public int count(Object... params) {
		return dao.count(params);
	}
	
	public boolean isExist(Object... params) {
		if(count(params)>0) {
			return true;
		} else {
			return false;
		}
	}
	
}
